﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace BYUStock.Comman
{
    public class CandleCollection
    {
        public List<Candle> Candles { get; set; }
        public DateTime StartTime { get; protected set; }
        public CandleSpan Span { get; protected set; }

        public CandleCollection(IEnumerable<Candle> candles = null, DateTime startTime = default, CandleSpan span = default)
        {
            this.Candles = new List<Candle>();
            if (candles != null)
                this.Candles.AddRange(candles);
            this.StartTime = startTime;
            this.Span = span;
        }
        public void Add(Candle candle)
        {
            Candles.Add(candle);
        }
        public void Add(IEnumerable<Candle> candles)
        {
            Candles.AddRange(candles);
        }
        public void Insert(Candle candle)
        {
            StartTime = StartTime - CandleParser.GetCandleTiemSpan(Span);
            Candles.Insert(0, candle);
        }
        public void Insert(IEnumerable<Candle> candles)
        {
            var count = candles.Count();
            var start = StartTime;
            var timeSpan = CandleParser.GetCandleTiemSpan(Span);
            for (int i = 0; i < count; i++)
            {
                start = start.Subtract(timeSpan);
            }
            StartTime = start;
            Candles = candles.Concat(Candles).ToList();
        }
        public void Skip(int count)
        {
            Candles = Candles.Skip(count).ToList();
        }
        public void Remove(int startIndex, int count = 0)
        {
            if (startIndex < 0 || count < 0) return;
            if (count == 0)
            {
                count = Candles.Count - startIndex;
            }
            Candles.RemoveRange(startIndex, count);
        }

    }
}
